﻿using SF.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SF.Ucenter.admin.statistics
{
    public partial class monthapp : System.Web.UI.Page
    {
        protected string[] orders_Tendency = new string[] { "", "",""};
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session[DTKeys.SESSION_APP_INFO] != null)
            {
                string appNum = Session[DTKeys.SESSION_APP_INFO].ToString(); 
                orders_Tendency = GetOrdersTendency(30, appNum);
            }
            else
            {
                orders_Tendency = GetOrdersTendency(30, "");
            }
        }

        private string[] GetOrdersTendency(int nday, string appNum)
        {
            //获取最近n天的统计数据
            DateTime startday = DateTime.Now.AddDays(-nday);
            DateTime endDay = DateTime.Now.AddDays(-1);
            string strStartTime = startday.ToString("yyyy-MM-dd") + " 00:00:00.000";
            string strEndTime = endDay.ToString("yyyy-MM-dd") + " 23:59:59.999";


            BLL.sf_goods_order bll = new BLL.sf_goods_order();
            System.Data.DataSet dsOrders = bll.GetList(" addTime >= '" + strStartTime + "' and addTime <= '" + strEndTime + "' ");
            if (appNum!="")
            {
                dsOrders = bll.GetList(" ispay = 2 and appNum = '" + appNum + "' and addTime >= '" + strStartTime + "' and addTime <= '" + strEndTime + "' ");
            }

            string xAxis = "";
            string yAxis0 = "";
            string yAxis1 = "";
            for (int i = 0; i < nday; i++)
            {
                string strStartTime1 = startday.AddDays(i).ToString("yyyy-MM-dd") + " 00:00:00.000";
                string strEndTime1 = startday.AddDays(i).ToString("yyyy-MM-dd") + " 23:59:59.999";
                System.Data.DataRow[] rows = dsOrders.Tables[0].Select(" addTime >= '" + strStartTime1 + "' and addTime <= '" + strEndTime1 + "'");
                xAxis += "'" + startday.AddDays(i).ToString("MM-dd") + "',";
                yAxis0 += rows.Length + ",";

                decimal totalMoney = 0;
                foreach (System.Data.DataRow r in rows) 
                {
                    totalMoney += decimal.Parse(r["totalMoney"].ToString());
                }
                yAxis1 += totalMoney + ",";
            }

            if (xAxis.Length > 0)
            {
                xAxis = xAxis.Remove(xAxis.Length - 1, 1);
                yAxis0 = yAxis0.Remove(yAxis0.Length - 1, 1);
                yAxis1 = yAxis1.Remove(yAxis1.Length - 1, 1);
            }

            return new[] { xAxis, yAxis0, yAxis1 };
        }
    }
}